/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditorTagsWindow.java
 *
 * Created on 08-may-2011, 1:37:52
 */
package GUI;

import Biblioteca.*;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.Image;

/**
 * Clase que maneja el aspecto grafico del editor de tags./n
 * Contiene la cancion leida en ese instante.
 * Tendrá una clase EditorTags que maneja el guardado de los tags aqui modificados
 * Implementación de tags como hashmap
 */
public class EditorTagsWindow extends javax.swing.JFrame {

    /**
     * representa la canción leida y a modificar.
     */
    private Cancion cancionActual;

    /**
     * representa la cancion original (la posicion donde estaba originalmente)
     */
    private int indiceCancion;
    /**
     * Representa al formulario principal playerWindow
     */
    private PlayerWindow ventanaPlayer;
    /**
     * representa la parte logica del editor de tags.
     */
    private EditorTags editor;

    /** Creates new form EditorTagsWindow */
    public EditorTagsWindow() {
        initComponents();
    }

    /** Crea el form y lee los datos de la canción que se le ha pasado como parametro.
     *  @param cancion la cancion que recibe donde modificar los datos.
     * @param vPlayer
     */
    public EditorTagsWindow(Cancion cancion, PlayerWindow vPlayer) {
        initComponents();
        cancionActual = cancion;
        ventanaPlayer = vPlayer;
        editor = new EditorTags();
        pintarInfoCancionActual();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tituloTextField = new javax.swing.JTextField();
        tituloLabel = new javax.swing.JLabel();
        artistaLabel = new javax.swing.JLabel();
        artistaTextField = new javax.swing.JTextField();
        albumLabel = new javax.swing.JLabel();
        albumTextField = new javax.swing.JTextField();
        añoLabel = new javax.swing.JLabel();
        añoTextField = new javax.swing.JTextField();
        pistaLabel = new javax.swing.JLabel();
        pistaTextField = new javax.swing.JTextField();
        generoLabel = new javax.swing.JLabel();
        generoTextField = new javax.swing.JTextField();
        infoLabel = new javax.swing.JLabel();
        dataLabel = new javax.swing.JLabel();
        data2Label = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        aplyButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        caratulaLabel = new javax.swing.JLabel();
        caratulaImage = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Editor de Tags");
        setResizable(false);

        tituloTextField.setBackground(new java.awt.Color(102, 102, 102));
        tituloTextField.setForeground(new java.awt.Color(240, 240, 240));

        tituloLabel.setText("Título");

        artistaLabel.setText("Artista");

        artistaTextField.setBackground(new java.awt.Color(102, 102, 102));
        artistaTextField.setForeground(new java.awt.Color(240, 240, 240));

        albumLabel.setText("Álbum");

        albumTextField.setBackground(new java.awt.Color(102, 102, 102));
        albumTextField.setForeground(new java.awt.Color(240, 240, 240));

        añoLabel.setText("Año");

        añoTextField.setBackground(new java.awt.Color(102, 102, 102));
        añoTextField.setForeground(new java.awt.Color(240, 240, 240));

        pistaLabel.setText("Pista");

        pistaTextField.setBackground(new java.awt.Color(102, 102, 102));
        pistaTextField.setForeground(new java.awt.Color(240, 240, 240));

        generoLabel.setText("Género");

        generoTextField.setBackground(new java.awt.Color(102, 102, 102));
        generoTextField.setForeground(new java.awt.Color(240, 240, 240));

        infoLabel.setText("Info");

        dataLabel.setText("Información sobre el archivo.");

        data2Label.setText("Códec");

        okButton.setText("Aceptar");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        aplyButton.setText("Aplicar");
        aplyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aplyButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancelar");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        caratulaLabel.setText("Carátula");

        caratulaImage.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        caratulaImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CDArt.jpg"))); // NOI18N
        caratulaImage.setToolTipText("Haga clic para modificar la Carátula del Álbum");
        caratulaImage.setAutoscrolls(true);
        caratulaImage.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        caratulaImage.setFocusable(false);
        caratulaImage.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                caratulaImageMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(151, Short.MAX_VALUE)
                .addComponent(okButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(aplyButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelButton)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tituloLabel)
                    .addComponent(artistaLabel)
                    .addComponent(albumLabel)
                    .addComponent(añoLabel)
                    .addComponent(pistaLabel)
                    .addComponent(infoLabel)
                    .addComponent(caratulaLabel)
                    .addComponent(generoLabel))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(generoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dataLabel)
                    .addComponent(data2Label)
                    .addComponent(pistaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(añoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(albumTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(artistaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tituloTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(caratulaImage, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tituloLabel)
                    .addComponent(tituloTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(artistaLabel)
                    .addComponent(artistaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(albumLabel)
                    .addComponent(albumTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(añoLabel)
                    .addComponent(añoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pistaLabel)
                    .addComponent(pistaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(generoLabel)
                    .addComponent(generoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(caratulaLabel)
                    .addComponent(caratulaImage, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(infoLabel)
                    .addComponent(dataLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(data2Label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(aplyButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        limpiarInfoCancion();
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void aplyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aplyButtonActionPerformed
        guardarCambios();
    }//GEN-LAST:event_aplyButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        guardarCambios();
        dispose();
    }//GEN-LAST:event_okButtonActionPerformed

    private void caratulaImageMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_caratulaImageMouseClicked
        JFileChooser fc = new JFileChooser();
        fc.setApproveButtonText("Modificar");
        fc.setDialogTitle("Modificar Caratula");
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Archivo Imagen", "jpg");
        fc.addChoosableFileFilter(filter);
        fc.removeChoosableFileFilter(fc.getAcceptAllFileFilter());
        int result = fc.showOpenDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) {
            String rutaImagen = fc.getSelectedFile().getPath();
            ImageIcon icon = new ImageIcon(rutaImagen);
            Image img = icon.getImage();
            Image newimg = img.getScaledInstance(160, 160, java.awt.Image.SCALE_SMOOTH);
            ImageIcon newIcon = new ImageIcon(newimg);
            this.caratulaImage.setIcon(newIcon);

        }
    }//GEN-LAST:event_caratulaImageMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel albumLabel;
    private javax.swing.JTextField albumTextField;
    private javax.swing.JButton aplyButton;
    private javax.swing.JLabel artistaLabel;
    private javax.swing.JTextField artistaTextField;
    private javax.swing.JLabel añoLabel;
    private javax.swing.JTextField añoTextField;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel caratulaImage;
    private javax.swing.JLabel caratulaLabel;
    private javax.swing.JLabel data2Label;
    private javax.swing.JLabel dataLabel;
    private javax.swing.JLabel generoLabel;
    private javax.swing.JTextField generoTextField;
    private javax.swing.JLabel infoLabel;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel pistaLabel;
    private javax.swing.JTextField pistaTextField;
    private javax.swing.JLabel tituloLabel;
    private javax.swing.JTextField tituloTextField;
    // End of variables declaration//GEN-END:variables

    /**
     * Este método pinta en los contenedores apropiados la meta-información de la canción seleccionada actualmente.
     */
    private void pintarInfoCancionActual() {

        String año = "Desconocido";
        String pista = "Desconocido";
        String kbps = "?";
        String canales = "Desconocidos";
        String codec = "Desconocido";

        if (cancionActual.getTag("año") != null) {
            año = cancionActual.getTag("año");
        }
        if (cancionActual.getTag("pista") != null) {
            pista = cancionActual.getTag("pista");
        }
        if (cancionActual.getTag("bitrate") != null) {
            kbps = cancionActual.getTag("bitrate");
        }
        if (cancionActual.getTag("canales") != null) {
            canales = cancionActual.getTag("canales");
        }
        if (cancionActual.getTag("codec") != null) {
            codec = cancionActual.getTag("codec");
        }

        añoTextField.setText(año);
        pistaTextField.setText(pista);
        String info = kbps + " kbps - Canales: " + canales;
        String info2 = "Códec: " + codec;
        dataLabel.setText(info);
        data2Label.setText(info2);

        tituloTextField.setText(cancionActual.getTitulo());
        artistaTextField.setText(cancionActual.getArtista().getNombre());
        albumTextField.setText(cancionActual.getAlbum().getNombre());
        generoTextField.setText(cancionActual.getGenero().getNombre());
        
        //caratulaImage.setIcon(new ImageIcon(cancionActual.getTag("caratula")));


    }

    /**
     * Este método limpia los campos donde se pinta la meta-información en pintarInfoCancionActual()
     */
    private void limpiarInfoCancion() {
        tituloTextField.setText(null);
        artistaTextField.setText(null);
        albumTextField.setText(null);
        añoTextField.setText(null);
        generoTextField.setText(null);
        pistaTextField.setText(null);
        caratulaImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CDArt.jpg")));
        dataLabel.setText(null);
        data2Label.setText(null);
    }

    /**
     * Este método guarda en una CancionHash los tags de los campos modificados en el editor, y se lo pasa a la clase EditorTags
     */
    private void guardarCambios() {
        //TODO guardar caratula, cambiar info de canciones editadas en al lista

        Cancion cancionAntigua = new Cancion(cancionActual.getRuta(), cancionActual.getTitulo(), cancionActual.getArtista(), cancionActual.getAlbum(), cancionActual.getGenero());
        cancionActual.setTitulo(tituloTextField.getText());
        Artista artista = new Artista(artistaTextField.getText());
        cancionActual.setArtista(artista);
        Album album = new Album(albumTextField.getText());
        cancionActual.setAlbum(album);
        Genero genero = new Genero(generoTextField.getText());
        cancionActual.setGenero(genero);
        cancionActual.setTag("año", añoTextField.getText());
        cancionActual.setTag("pista", pistaTextField.getText());

        //cancionActual.setTag("caratula", caratulaImage.getIcon()));
        editor.edit(cancionActual);
        ventanaPlayer.actualizaInfoCancion(cancionAntigua, cancionActual);
        //cambiar la info de las canciones editadas en la lista
    }
}
